package com.project.bookstore.config.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

@Configuration
//启用方法前进行权限认证
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    private JwtAuthenticationFilter jwtAuthenticationFilter;

    //注册中心
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable(); // 解决POST请求403错误
        http.cors();    //支持cors

        //放行的 urls
        String[] urls = {
                "/**/*",            //全部开放
                "/static/img/**/*", //图片资源相关
                "/user/register",   //注册账号相关
                "/user/username/*",
                "/user/state/*",    //登录状态验证
                "/user/login",      //登录
                "/favicon.ico",     //knife4j 相关
                "/doc.html",
                "/**/*.css",
                "/**/*.js",
                "/swagger-resources",
                "/v2/api-docs"
        };

        http.authorizeRequests()
                .antMatchers(urls).permitAll()
                .anyRequest().authenticated();
        //注册自己的 jwt 授权过滤器
        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }
    //返回 AuthenticationManager 从而接收框架的认证工作
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
    //注册源
    @Bean
    protected CorsConfigurationSource corsConfigurationSource(){
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许从百度站点跨域
        configuration.setAllowedOrigins(Arrays.asList("http://www.baidu.com","http://localhost:8080"));
        // 允许使用方法
        configuration.setAllowedMethods(Arrays.asList("GET","POST","PUT","DELETE"));
        // 允许带凭证
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //对所有URL生效
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

    //往容器中放 加密器 BCrypt
    @Bean
    protected PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
